/*
 * Created by: vadikv
 * Created: Wednesday, March 05, 2008
 */

using System;
using System.Collections.Generic;
using System.Text;
using OneDayModel.Interfaces;
using OneDayModel.Model;
using OneDayModel.PresentationLogic;
using OneDayModel.Tests.Mocks;

namespace OneDayModel.Tests.Fits.UIUsers
{
    public class NewUserProfileUser : INewUserProfileUI
    {
        private string availableRoles;
        private readonly List<AttributeRecord> roleAttributes = new List<AttributeRecord>();
        private bool newEMailOptionShown;
        private bool retrieveCredantialsShown;
        private string[] missedAttributesShown;
        private bool backToQuestionerShown;

        public event EventHandler<RolesSelectionArgs> onRolesSelection;
        public event EventHandler<SaveProfileArgs> onSaveProfile;
        public event EventHandler<SaveProfileArgs> onReturnToQuestioner;


        public NewUserProfileUser()
        {
            new NewUserProfileUILogic(this, new NewUserProfileModel(new MockRepository()));
        }

        public string AvailableRoles
        {
            get { return availableRoles; }
        }

        public string ShownRequiredAttributes
        {
            get
            {
                return GetShownAttributes(true);
            }
        }

        public string ShownOptionalAttributes
        {
            get
            {
                return GetShownAttributes(false);
            }
        }

        public string NewEMailOptionShown
        {
            get { return newEMailOptionShown ? "yes":"no"; }
        }

        public string RetrieveCredantialsShown
        {
            get { return retrieveCredantialsShown ? "yes" : "no"; }
        }

        public string MissedAttributesInfoShown
        {
            get
            {
                StringBuilder missedAttributesInfoShown = new StringBuilder();
                if (missedAttributesShown.Length > 0)
                {
                    missedAttributesInfoShown.Append(missedAttributesShown[0]);
                    for (int i = 1; i < missedAttributesShown.Length; i++)
                    {
                        missedAttributesInfoShown.Append(", ");
                        missedAttributesInfoShown.Append(missedAttributesShown[i]);
                    }
                }

                return missedAttributesInfoShown.ToString();
            }
        }

        private string GetShownAttributes(bool required)
        {
            StringBuilder builder = new StringBuilder();
            foreach (AttributeRecord attribute in roleAttributes)
            {
                if (attribute.RoleAttribute.Required == required)
                {
                    if (builder.Length > 0)
                        builder.Append(";");
                    builder.Append(attribute.RoleAttribute.DisplayName);

                    if (!string.IsNullOrEmpty(attribute.Value))
                    {
                        builder.Append("=");
                        builder.Append(attribute.Value);
                    }
                }
            }
            return builder.ToString();
        }

        public void SetAvailableRoles(string[] roles)
        {
            StringBuilder builder = new StringBuilder();
            foreach (string role in roles)
            {
                if (builder.Length > 0)
                    builder.Append(";");
                builder.Append(role);
            }
            availableRoles = builder.ToString();
        }

        public void ShowAttributes(IEnumerable<IAttributeRecord> attributes)
        {
            roleAttributes.Clear();

            foreach (AttributeRecord attribute in attributes)
            {
                if (attribute.RoleAttribute.Visible)
                    roleAttributes.Add(attribute);
            }
        }

        public void ShowNewEMailOption()
        {
            newEMailOptionShown = true;
        }

        public void ShowRetrieveCredantials(string email)
        {
            retrieveCredantialsShown = true;
        }

        public void ShowMissingAttributesInfo(string[] missedAttributes)
        {
            missedAttributesShown = missedAttributes;
        }

        public void ShowBackToQuestioner()
        {
            backToQuestionerShown = true;
        }

        public void SetSelectedRoles(string[] selectedRoles)
        {
            onRolesSelection(this, new RolesSelectionArgs(selectedRoles));
        }


        public void SaveProfile()
        {
            onSaveProfile(this, new SaveProfileArgs(roleAttributes.ToArray()));
        }

        public string BackToQuestionerShown
        {
            get { return backToQuestionerShown ? "yes" : "no"; }
        }

        public void ReturnToQuestioner()
        {
            onReturnToQuestioner(this, new SaveProfileArgs(roleAttributes.ToArray()));
        }

        public void FillAttributes(KeyValuePair<string, string>[] attributes)
        {
            foreach (KeyValuePair<string, string> pair in attributes)
            {
//                var roleAttribute = from attribute in roleAttributes
//                                    where attribute.RoleAttribute.DisplayName == pair.Key
//                                    select attribute;
                foreach (AttributeRecord record in roleAttributes)
                {
                    if (record.RoleAttribute.DisplayName == pair.Key)
                    {
                        record.Value = pair.Value;
                        break;
                    }
                }
            }
        }
    }
}